<template>
  <div class="wrapper">
    <div class="login-box">
      <h1>超市管理系统</h1>
      <el-form :model="loginForm" status-icon :rules="rules" ref="ruleForm" label-width="60px" class="demo-ruleForm">
        <el-form-item label="账号" prop="username">
          <el-input type="text" v-model="loginForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="verifycode">
          <el-row :span="24">
            <el-col :span="13">
              <el-input v-model="loginForm.code" auto-complete="off" placeholder="请输入验证码" size=""></el-input>
            </el-col>
            <el-col :span="11">
              <div class="login-code" width="100%" @click="refreshCode">
                <Identify :identifyCode="identifyCode"></Identify>
              </div>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button type="success" plain @click="submitForm('ruleForm')">登陆</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Identify from "./LoginCode.vue"
import jwt from "jwt-decode"
import { mapMutations } from "vuex"
export default {
  components: {
    Identify
  },
  data () {
    var validateUser = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"))
      } else {
        callback()
      }
    }
    return {
      identifyCodes: '1234567890abcdefjhijklinopqrsduvwxyz',// 随机串内容
      identifyCode: '',
      info: "",
      loginForm: {
        username: "",
        password: "",
        code: ""
      },
      rules: {
        username: [{ validator: validateUser, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }]

      },
    }
  },

  mounted () {
    // 初始化验证码
    this.identifyCode = ''
    this.makeCode(this.identifyCodes, 4)
  },

  methods: {
    // 重置验证码
    refreshCode () {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
    },
    makeCode (o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes.length)]
      }
    },
    randomNum (min, max) {
      return Math.floor(Math.random() * (max - min) + min)
    },

    ...mapMutations("loginModule", ["setUser"]),
    submitForm (formName) {
      if (this.loginForm.code.toLowerCase() !== this.identifyCode.toLowerCase()) {
        this.$message.error('请填写正确验证码')
        this.refreshCode()
        return
      }

      this.$refs[formName].validate((valid) => {
        if (valid) {
          let { username, password } = this.loginForm
          // 请求登录接口-------------
          this.$api
            .getLogin({
              username,
              password,
            })
            .then((res) => {
              if (res.data.status === 200) {
                let obj = {
                  user: jwt(res.data.data).username,
                  token: res.data.data,
                }
                this.setUser(obj)
                // 存储本地
                localStorage.setItem("user", JSON.stringify(obj))
                this.$router.push("/")
              } else {
                this.$message.error("账号或密码错误")
              }
            })
        } else {
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
      this.loginForm.code = ''
    },
  },
}
</script>

<style lang="less" scoped>
.wrapper {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  background: url('../../assets/1.jpg');
  background-size: 120%;
}
.login-box {
  text-align: center;
  width: 400px;
  height: 320px;
  padding: 20px;
  margin: 0 auto;
  margin-top: 120px;
  border-radius: 10px;
  background: rgba(253, 253, 253, 0.5);
  position: relative;
}
.demo-ruleForm {
  margin-top: 40px;
  margin-right: 20px;
}
h1 {
  color: rgb(46, 43, 43);
}
.el-button {
  margin-right: 20px;
}
</style>
